<!--
 * @Descripttion:
 * @version:
 * @Author: cxguo
 * @Date: 2020-06-15 17:45:55
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-12 14:44:42
-->
<template>
  <div class="app-container">
    <el-form
      ref="goodBase"
      :model="dataObj"
      :label-position="'right'"
      label-width="60px"
      inline
    >
      <div>
        基本业务
        <el-divider />
        <div class="margin-t20">
          <el-checkbox v-model="dataObj.isAllowNegStock" true-label="1" false-label="0">
            允许负库存销售
          </el-checkbox>
          <span class="color-gay-shallow font12">( 开启后支持无库存销售 )</span>
        </div>
        <div class="margin-t20">
          <el-checkbox v-model="dataObj.isValiMinSalePrice" true-label="1" false-label="0">
            销售时检查最低销售价
          </el-checkbox>
          <span class="color-gay-shallow font12">( 开启后，销售价低于最低售价时，系统会自动提醒 )</span>
        </div>
        <!-- <div class="margin-t20">
          <el-checkbox v-model="dataObj.isOpenSerial" true-label="1" false-label="0">
            启用商品序列号
          </el-checkbox><br>
          <span class="color-gay-shallow font12">( 电子及其他行业中，用序列号作为商品的唯一性标识，启用商品序列号可以帮助您跟踪和管理商品。 )</span>
        </div> -->
      </div>
      <div class="margin-t40">
        税率设置
        <el-divider />
        <div class="margin-t20">
          <el-checkbox v-model="dataObj.isOpenPurchaseTax" true-label="1" false-label="0">
            启用进货税率
          </el-checkbox>
          <span class="color-gay-shallow font12">默认税率 </span>
          <el-input v-model="dataObj.purchaseTax" class="w100" /> %
        </div>
        <div class="margin-t20">
          <el-checkbox v-model="dataObj.isOpenSaleTax" true-label="1" false-label="0">
            启用销售税率
          </el-checkbox>
          <span class="color-gay-shallow font12">默认税率 </span>
          <el-input v-model="dataObj.saleTax" class="w100" /> %
        </div>
        <div class="text-center">
          <el-button
            type="primary"
            :loading="btnLoading.save"
            @click="btnSaveData"
          >保 存</el-button>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import { oftenuse } from '@/libs/mixins'
import { getBusinessSetup as getData, saveBusinessSetup as saveData } from '@/api/sys/busetup.js'
import store from '@/store'

export default {
  name: 'BusinessSetup',
  mixins: [oftenuse],
  data() {
    return {
      dataObj: { isAllowNegStock: '0', isValiMinSalePrice: '0', isOpenSerial: '0', isOpenPurchaseTax: '0', isOpenSaleTax: '0' }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    btnSaveData() {
      const cachedViews = this.$store.state.tagsView.cachedViews || []
      if (cachedViews.length > 1) {
        return this.$message.error('请先关闭其他窗口后，再进行保存！')
      }
      const value = JSON.stringify(this.dataObj)
      const params = { value }
      this.setBtnLoading('save', true)
      this.saveData(params).then(res => {
        this.setBtnLoading('save', false)
        if (res.data.flag) {
          store.dispatch('user/setBusSetup', this.dataObj)
        }
      })
    },
    saveData(params) {
      return new Promise((resovel, reject) => {
        saveData(params).then(res => {
          resovel(res)
          if (res.data.flag) {
            this.$notify.success({ title: '保存成功！' })
          } else {
            this.$notify.error({ title: '保存失败！' })
          }
        })
      })
    },
    getData() {
      getData().then(res => {
        if (res.data.flag) {
          const data = JSON.parse(res.data.data)
          this.dataObj = data
        }
      })
    }
  }
}
</script>

<style>
.el-divider--horizontal{
  margin: 5px 0;
}
</style>
